<template>
  <div class="m-4 mr-0 overflow-hidden bg-white">
    <BasicTree
      title="部门列表"
      toolbar
      search
      treeWrapperClassName="h-[calc(100%-35px)] overflow-auto"
      :clickRowToExpand="false"
      :treeData="treeData"
      :fieldNames="{ key: 'deptId', title: 'name' }"
      @select="handleSelect"
    />
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, ref } from 'vue';

  import { BasicTree, TreeItem } from '@/components/Tree';
  import { getAllDeptListApi } from '@/api/sys/sysData';
  import {useMessage} from "@/hooks/web/useMessage";

  defineOptions({ name: 'DeptTree' });

  const emit = defineEmits(['select']);
  const { createMessage } = useMessage();

  const treeData = ref<TreeItem[]>([]);

  async function fetch() {
    await getAllDeptListApi().then(res=>{
      if(res.code==200){
        treeData.value=res.data;
      }else{
        createMessage.error(res.message);
      }
    })

  }

  function handleSelect(keys) {
    emit('select', keys[0]);
  }

  onMounted(() => {
    fetch();
  });
</script>
